const fs = require("fs");
const arealist = require("./arealist");

const province_list = arealist["province_list"];
const city_list = arealist["city_list"];
const county_list = arealist["county_list"];

const province_list_keys = Object.keys(province_list);
const city_list_keys = Object.keys(city_list);
const county_list_keys = Object.keys(county_list);

const city_obj = {};

function buildAreaList() {
  city_list_keys.map(city_key => {
    city_obj[city_key] = {
      code: city_key,
      label: city_list[city_key],
      value: city_key
    };
    const code = city_key.substring(0, 4);
    const children = [];
    county_list_keys.map(county_key => {
      if (county_key.substring(0, 4) === code) {
        children.push({
          code: county_key,
          label: county_list[county_key],
          value: county_key
        });
      }
    });
    city_obj[city_key]["children"] = children;
  });

  const area_arr = [];

  province_list_keys.map(province_key => {
    const province_obj = {
      code: province_key,
      label: province_list[province_key],
      value: province_key
    };
    const code = province_key.substring(0, 2);
    const children = [];
    city_list_keys.map(city_key => {
      if (city_key.substring(0, 2) === code) {
        children.push(city_obj[city_key]);
      }
    });
    province_obj["children"] = children;
    area_arr.push(province_obj);
  });

  fs.writeFileSync("arealist.json", JSON.stringify(area_arr));
}

function buildAreaTree() {
  city_list_keys.map(city_key => {
    city_obj[city_key] = {
      key: city_key,
      title: city_list[city_key],
      value: city_key
    };
    const code = city_key.substring(0, 4);
    const children = [];
    county_list_keys.map(county_key => {
      if (county_key.substring(0, 4) === code) {
        children.push({
          key: county_key,
          title: county_list[county_key],
          value: county_key
        });
      }
    });
    city_obj[city_key]["children"] = children;
  });

  const area_arr = [];

  province_list_keys.map(province_key => {
    const province_obj = {
      key: province_key,
      title: province_list[province_key],
      value: province_key
    };
    const code = province_key.substring(0, 2);
    const children = [];
    city_list_keys.map(city_key => {
      if (city_key.substring(0, 2) === code) {
        children.push(city_obj[city_key]);
      }
    });
    province_obj["children"] = children;
    area_arr.push(province_obj);
  });

  fs.writeFileSync("areatree.json", JSON.stringify(area_arr));
}

function buildCityList() {
  city_list_keys.map(city_key => {
    city_obj[city_key] = {
      code: city_key,
      label: city_list[city_key],
      value: city_key
    };
  });

  const area_arr = [];

  province_list_keys.map(province_key => {
    const province_obj = {
      code: province_key,
      label: province_list[province_key],
      value: province_key
    };
    const code = province_key.substring(0, 2);
    const children = [];
    city_list_keys.map(city_key => {
      if (city_key.substring(0, 2) === code) {
        children.push(city_obj[city_key]);
      }
    });
    province_obj["children"] = children;
    area_arr.push(province_obj);
  });

  fs.writeFileSync("citylist.json", JSON.stringify(area_arr));
}

function buildCityTree() {
  city_list_keys.map(city_key => {
    city_obj[city_key] = {
      key: city_key,
      title: city_list[city_key],
      value: `${city_key}@${city_list[city_key]}`
    };
  });

  const area_arr = [];

  province_list_keys.map(province_key => {
    const province_obj = {
      key: province_key,
      label: province_list[province_key],
      value: province_key
    };
    const code = province_key.substring(0, 2);
    const children = [];
    city_list_keys.map(city_key => {
      if (city_key.substring(0, 2) === code) {
        children.push(city_obj[city_key]);
      }
    });
    province_obj["children"] = children;
    area_arr.push(province_obj);
  });

  fs.writeFileSync("citytree.json", JSON.stringify(area_arr));
}

function buildCityValueWithLabelList() {
  city_list_keys.map(city_key => {
    city_obj[city_key] = {
      code: city_key,
      label: city_list[city_key],
      value: `${city_key}-${city_list[city_key]}`
    };
  });

  const area_arr = [];

  province_list_keys.map(province_key => {
    const province_obj = {
      code: province_key,
      label: province_list[province_key],
      value: province_key
    };
    const code = province_key.substring(0, 2);
    const children = [];
    city_list_keys.map(city_key => {
      if (city_key.substring(0, 2) === code) {
        children.push(city_obj[city_key]);
      }
    });
    province_obj["children"] = children;
    area_arr.push(province_obj);
  });

  fs.writeFileSync("citylistvaluewithlabel.json", JSON.stringify(area_arr));
}

buildCityValueWithLabelList();
